package net.wantongtech.xa.uploadbasicinfo.dao;

import cn.com.taiji.sdk.model.comm.protocol.sdts.backlist.CardBlackListDownloadModel;
import com.wtkj.dao.impl.BaseDataTemplate;
import com.wtkj.vo.RandomGUID;
import org.joda.time.LocalDateTime;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.stereotype.Repository;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

/**
 * 重新填充卡片黑名单记录DAO实现
 *
 * @author MaShibo
 * 2021/2/20 0020
 */
@Repository
public class ReFillCardBlackListRecordDao extends BaseDataTemplate {


    public void batchInsertCardBlackRecord(List<CardBlackListDownloadModel> modelList, String version) {
        final String sql =
                "INSERT INTO bl_cardrecord_tmp\n" +
                        "  (listid,\n" +
                        "   inserttime,\n" +
                        "   issuerid,\n" +
                        "   creationtime,\n" +
                        "   cardid,\n" +
                        "   status,\n" +
                        "   TYPE,\n" +
                        "   version,\n" +
                        "   receivetime)\n" +
                        "VALUES\n  (?,   ?,   ?,   ?,   ?,   ?,   ?,   ?,   ?)";
        jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {

            @Override
            public void setValues(PreparedStatement ps, int i) throws SQLException {
                CardBlackListDownloadModel cardBlackListDownloadModel = modelList.get(i);
                ps.setString(1, new RandomGUID().ToRawString());
                String insertTime = cardBlackListDownloadModel.getInsertTime();
                ps.setTimestamp(2, new java.sql.Timestamp(LocalDateTime.parse(insertTime).toDate().getTime()));
                ps.setString(3, cardBlackListDownloadModel.getIssuerId());
                String creationTime = cardBlackListDownloadModel.getCreationTime();
                ps.setTimestamp(4, new java.sql.Timestamp(LocalDateTime.parse(creationTime).toDate().getTime()));
                ps.setString(5, cardBlackListDownloadModel.getCardId());
                ps.setInt(6, cardBlackListDownloadModel.getStatus());
                ps.setInt(7, cardBlackListDownloadModel.getType());
                ps.setString(8, version);
                ps.setTimestamp(9, new java.sql.Timestamp(new Date().getTime()));
            }

            @Override
            public int getBatchSize() {
                return modelList.size();
            }

        });
    }

    public void batchDeleteCardBlackRecord(final List<CardBlackListDownloadModel> cardBlackListDownloadModels) {
        final String sql = " DELETE  BL_CARDRECORD_TMP BL WHERE BL.ISSUERID=? AND BL.CARDID=? AND BL.TYPE=? ";
        jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement ps, int i) throws SQLException {
                CardBlackListDownloadModel cardBlackListDownloadModel = cardBlackListDownloadModels.get(i);
                ps.setString(1, cardBlackListDownloadModel.getIssuerId());
                ps.setString(2, cardBlackListDownloadModel.getCardId());
                ps.setInt(3, cardBlackListDownloadModel.getType());
            }

            @Override
            public int getBatchSize() {
                return cardBlackListDownloadModels.size();
            }
        });
    }


    public int queryCardBlackRecordCount() {
        final String sql = " SELECT COUNT(1) FROM bl_cardrecord_tmp";
        return jdbcTemplate.queryForObject(sql, Integer.class);
    }

    public String queryLatestCardBlackVersion() {
        final String sql = "select max(a.version) from bl_cardrecord_tmp a";
        return jdbcTemplate.queryForObject(sql, String.class);
    }

    public void truncateCardRecordTable() {
        final String sql = "truncate table bl_cardrecord_tmp";
        jdbcTemplate.execute(sql);
    }

}
